package pl.smath.renderer.model;

import gwt.g2d.client.graphics.Surface;
import gwt.g2d.client.graphics.canvas.Context;
import pl.smath.renderer.renderer.TermRendererException;
import pl.smath.renderer.utils.Cursor;
import pl.smath.renderer.utils.InteractionSocket;

import com.google.gwt.user.client.ui.RootPanel;

public class ConjugateView extends View {

	private View inner;
	private Double space;
	
	public ConjugateView(View inner) {
		super("*");
		this.inner = inner;
	}

	/**
	 * @param space the space to set
	 */
	public void setSpace(Double space) {
		this.space = space;
	}

	/**
	 * @return the space
	 */
	public Double getSpace() {
		return space;
	}
	
	/**
	 * render text at the current position
	 * @param canvas
	 */
	@Override
	public void render(Surface canvas, InteractionSocket socket) throws TermRendererException {
		//TODO brackets
		Surface surface = new Surface();
		surface.setFont(this.getFont());
		Cursor cursor = this.getCursor();
		Double leftOpenBracketWidth = 0d;
		
		if ((this.getFont() == null)||(this.getFontHeight() == null)) {
			this.setFont(canvas.getFont());
		}
		canvas.setFont(this.getFont());
		
		if (isNegative()) {
			this.minusSign.render(canvas, new Cursor(cursor.getX(), this.getCursor().getY() - this.getBaselineOffset()));
		}
		
		this.inner.render(canvas, socket);

		Double x = cursor.getX();
		Double y = cursor.getY() - getHeight() + getSpace();
		Double length = getWidth(); 
		
		Context ctx = canvas.getContext();
		
		ctx.beginPath();
		ctx.moveTo(x, y);
		ctx.lineTo(x + length, y);
		ctx.closePath();
		
		ctx.stroke();
		
	}

	/**
	 * set the position on canvas to render
	 * @param Cursor
	 */
	@Override
	public void setCursor(Cursor cursor) {
		cursor.setY(cursor.getY());
		
		Surface surface = new Surface();
		RootPanel.get().add(surface);
		surface.setFont(this.getFont());

		
		surface.setFont(this.font);
		Double minusWidth = 0d;
		if (this.isNegative()) minusWidth = this.minusSign.getWidth();
		
		
		super.setCursor(new Cursor(cursor.getX(), cursor.getY()));
		
		if (this.isNegative()) {
			cursor.setX(cursor.getX() + minusWidth);
		}
		
		
		inner.setCursor(new Cursor(cursor.getX(), cursor.getY()));

		RootPanel.get().remove(surface);
	}
	
	/**
	 * @param font the font to set
	 */
	@Override
	public void setFont(String font) {
		this.inner.setFont(font);

		if (font.indexOf("px")!=-1) {
			this.setFontHeight(new Double(font.substring(0, font.indexOf("px"))));
			this.font = font.substring(font.indexOf("px ")+3);
			this.minusSign.setFont(this.font, this.getFontHeight());
			this.setSpace(this.getFontHeight() * 0.2d);
			update();
		} else {
			this.setFontHeight(null);
			this.font = null;
		}

	}
	
	/**
	 * updates the view after changes
	 */
	@Override
	public void update() {
		Surface surface = new Surface();
		RootPanel.get().add(surface);
		surface.setFont(this.getFont());

		
		surface.setFont(this.getFont());
		
		Double minusWidth = 0d;
		
		if (isNegative()) 
			minusWidth = this.minusSign.getWidth();

		
		this.setWidth(minusWidth + inner.getWidth());
		this.setHeight(inner.getHeight() + getSpace() );


		RootPanel.get().remove(surface);
	}
}
